#encoding=utf8
from pub import Func,RoomDict;
from pub.Config import Config

'''
对用户字典的操作

'''


def getAllOnlineUser():

    return Config.D_USER_DICT;
    

def checkUserInDict(oUser):
    if (not oUser):           
        return None;       
    try:
        return Config.D_USER_DICT.has_key[oUser.get_user_name()];
    except Exception, e:
        print str(e);

def getUserFromUserDictByUserName(UserName):
    '''
            根据用户名取得用户字典中的用户对象
    @param UserName:string 用户名
    @return: User() 
    '''
    if(Config.D_USER_DICT.has_key(UserName)):
        return Config.D_USER_DICT[UserName]
    else:
        return None;

def checkUserInDictByUserName(UserName):
    '''
    根据用户名检测用户字典中用户对象是否存在
    @param UserName:string 用户名
    @return:bool 
    '''
    return Config.D_USER_DICT.has_key[UserName];
def getUserFromUserDictById(UserId):
    '''
    根据用户名取得用户字典中的用户对象
    @param UserId:int    用户Id
    @return: User()
    '''
    result = None
    AllUsers = Config.D_USER_DICT
    for (key, tmpUser) in AllUsers.items():
        if(tmpUser != None):
            if(str(UserId) == str(tmpUser.get_user_id())):
                return  tmpUser
       
    return result
      
def addToUserDict(oUser):
    '''
    添加用户对象到字典
    '''
    if (not oUser):           
        return False;
    try:
        Config.D_USER_DICT[oUser.get_user_name()] = oUser;
        return True;
    except Exception, e:
        print str(e);
        return False;
def deleteUserFromDict(oUser):
    '''
    从用户字典中删除用户对象
    '''
    if(Config.D_USER_DICT.has_key(oUser.get_user_name())):
        del(Config.D_USER_DICT[oUser.get_user_name()]);
        return True;
    else:
        return False;





def getUserFromSocket(socketObj):
    '''
                        通过socket得到用户字典里的用户
        @param socketObj:
        @return: userObj; 
    '''
    if not socketObj:
        return None;
    for user in Config.D_USER_DICT.values():
        if user.get_socket() == socketObj:
            return user;        
    return None;
            
def userToSwitchRoom(userId, roomToId): 
    '''
        用户从原房间切换到roomTo房间
        @param userId:用户ID
        @param roomToId:新房间ID
        @return:bool    
    '''
    userId = Func.toInt(userId, 0);
    roomToId = Func.toInt(roomToId, 0);
    if userId <= 0:
        return False;
    if roomToId <= 0:
        return False;    
    room = RoomDict.getRoomFromDictById(roomToId);
    if not room:
        return False;    
    user = getUserFromUserDictById(userId);
    if not user:
        return False; 
    oldRoom = user.get_room();
    if oldRoom:
        RoomDict.delUserToRoom(user, oldRoom.get_id());       
    user.set_room(room);  
    if room.get_state() == Config.I_ROOM_STATE_START:#游戏中
        user.set_user_state(Config.I_USER_GAME_STATE_SEE);  
    else:
        user.set_user_state(Config.I_USER_GAME_STATE_OK);
    user.set_inroom(Config.I_SYS_ONE);
    addToUserDict(user);#更新用户字典
    return RoomDict.addOrModUserToRoom(user, roomToId);#更新用户到新房间    
